package rursu.dal.managers;

import java.util.ArrayList;
import java.util.List;

import rursu.business.pojo.MovieDetails;
import rursu.business.pojo.Person;
import rursu.dal.managers.interfaces.IMovieDetailsDAL;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MovieDetailsDALManager implements IMovieDetailsDAL {

	private SQLiteDatabase db;
	
	public static final String TABLE_NAME = "movie_details";
	
	protected static final String ID = "id";
	protected static final String CAST = "cast";	
	protected static final String TRAILER = "trailer";
	protected static final String LAST_MODIFIED = "last_modified";
	
	public MovieDetailsDALManager(SQLiteDatabase db) {
		this.db = db;		
	}
	
	public long insert(MovieDetails details) {
		ContentValues initialValues = new ContentValues();
		
		StringBuilder persons = new StringBuilder();
		
		for ( Person person : details.getCast() ) {
			persons.append(person.getName() + ", ");
		}
		
		// delete redundant ','
		if (persons.length() >= 2) {
			persons.delete( persons.length() - 2, persons.length() - 1 );
		}
		
        initialValues.put( MovieDetailsDALManager.ID, details.getId() );
        initialValues.put( MovieDetailsDALManager.CAST, persons.toString() );
        initialValues.put( MovieDetailsDALManager.TRAILER, details.getTrailer() );
        initialValues.put( MovieDetailsDALManager.LAST_MODIFIED, details.getLastModified() );

        return db.insert(TABLE_NAME, null, initialValues);
	}

	public void delete(int id) {
		db.delete(TABLE_NAME, MovieDetailsDALManager.ID + "=" + id, null);		
	}

	public MovieDetails get(int id) {
		Cursor cursor = db.query(TABLE_NAME, new String [] {
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.CAST,				
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.LAST_MODIFIED,
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.TRAILER },
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.ID + "=" + id, null, null, null, null, "1");

		MovieDetails details = null;
		
		if ( cursor.getCount() > 0 ) {
		
			details = new MovieDetails();
			cursor.moveToFirst();
			
			details.setTrailer( cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.TRAILER ) ) );
			details.setLastModified( cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.LAST_MODIFIED ) ) );
			
			String cast = cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.CAST ) );
			
			List<Person> persons = new ArrayList<Person>();
			
			for ( String personString : cast.split(",") ) {
				Person p = new Person();
				p.setName( personString.trim() );
				persons.add( p );
			}
			
			details.setCast( persons );
			details.setId( id );
		}
		
		cursor.close();
		return details;
	}

	public List<MovieDetails> getAll() {
		Cursor cursor = db.query(TABLE_NAME, new String [] {
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.CAST,
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.LAST_MODIFIED,
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.TRAILER, 
				MovieDetailsDALManager.TABLE_NAME + "." + MovieDetailsDALManager.ID}, null, null, null, null, null);

		List<MovieDetails> listDetails = new ArrayList<MovieDetails>();
		
		cursor.moveToFirst();
		
		while ( !cursor.isAfterLast() ) {
			
			MovieDetails details = new MovieDetails();
			
			details.setTrailer( cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.TRAILER ) ) );
			details.setId( cursor.getInt( cursor.getColumnIndex( MovieDetailsDALManager.ID ) ) );
			details.setLastModified( cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.LAST_MODIFIED ) ) );
			
			String cast = cursor.getString( cursor.getColumnIndex( MovieDetailsDALManager.CAST ) );
			
			List<Person> persons = new ArrayList<Person>();
			
			for ( String personString : cast.split(",") ) {
				Person p = new Person();
				p.setName( personString.trim() );
				persons.add( p );
			}
			
			details.setCast( persons );
			
			listDetails.add( details );
			
			cursor.moveToNext();
		}
		
		cursor.close();
		
		return (listDetails.isEmpty()) ? null : listDetails;
	}

	public int update(MovieDetails details) {
		
		StringBuilder persons = new StringBuilder();
		
		for ( Person person : details.getCast() ) {
			persons.append(person.getName() + ", ");
		}
		
		// delete redundant ','
		persons.delete( persons.length() - 2, persons.length() - 1 );
		
		ContentValues values = new ContentValues();
		
		values.put( MovieDetailsDALManager.ID, details.getId() );
        values.put( MovieDetailsDALManager.CAST, details.getCast().toString() );
        values.put( MovieDetailsDALManager.TRAILER, details.getTrailer() );
        values.put( MovieDetailsDALManager.LAST_MODIFIED, details.getLastModified() );
        
		return db.update(TABLE_NAME, values, MovieDetailsDALManager.ID, null);
	}

}